package com.xxx.video.mine.fragments;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.xxx.video.R;
import com.xxx.video.commen.configs.UserManager;
import com.xxx.video.commen.fragments.BaseFragment;
import com.xxx.video.commen.utils.ResourcesUtils;
import com.xxx.video.commen.utils.StatusBarUtil;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.login.activitys.LoginActivity;
import com.xxx.video.login.beans.UserInfoBean;
import com.xxx.video.mine.activitys.AccountInformationActivity;
import com.xxx.video.mine.activitys.BrowseRecordsActivity;
import com.xxx.video.mine.activitys.CommentManagementActivity;
import com.xxx.video.mine.activitys.CreationCenterActivity;
import com.xxx.video.mine.activitys.DownloadManagementActivity;
import com.xxx.video.mine.activitys.FavoritesActivity;
import com.xxx.video.mine.activitys.FeedBackActivity;
import com.xxx.video.mine.activitys.LikeRecordActivity;
import com.xxx.video.mine.activitys.SettingActivity;
import com.xxx.video.mine.presenters.IMineView;
import com.xxx.video.mine.presenters.MinePresenter;


import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

public class MineFragment extends BaseFragment<MinePresenter> implements IMineView {

    @BindView(R.id.cl_mine_fragment_actionbar)
    ConstraintLayout clMineFragmentActionbar;
    @BindView(R.id.iv_mine_fragment_user_avatar)
    ImageView ivMineFragmentUserAvatar;
    @BindView(R.id.tv_mine_fragment_login_register)
    TextView tvMineFragmentLoginRegister;
    @BindView(R.id.tv_mine_fragment_user_name)
    TextView tvMineFragmentUserName;
    @BindView(R.id.tv_mine_fragment_user_star_download)
    TextView tvMineFragmentUserStarDownload;
    @BindView(R.id.ll_mine_fragment_user_info)
    LinearLayout llMineFragmentUserInfo;
    @BindView(R.id.cl_mine_fragment_user)
    ConstraintLayout clMineFragmentUser;
    @BindView(R.id.tv_mine_fragment_creation_center)
    TextView tvMineFragmentCreationCenter;
    @BindView(R.id.tv_mine_fragment_browse_records)
    TextView tvMineFragmentBrowseRecords;
    @BindView(R.id.tv_mine_fragment_like_record)
    TextView tvMineFragmentLikeRecord;
    @BindView(R.id.tv_mine_fragment_download_management)
    TextView tvMineFragmentDownloadManagement;
    @BindView(R.id.tv_mine_fragment_comment_management)
    TextView tvMineFragmentCommentManagement;
    @BindView(R.id.tv_mine_fragment_feed_back)
    TextView tvMineFragmentFeedBack;

    @Override
    protected MinePresenter createPresenter() {
        return new MinePresenter(Objects.requireNonNull(getActivity()), this);
    }

    public static MineFragment newInstance(Bundle args) {
        MineFragment fragment = new MineFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initToolbarData() {
        super.initToolbarData();
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) clMineFragmentActionbar.getLayoutParams();
        layoutParams.setMargins(0, StatusBarUtil.getInstance().getStatusBarHeight(Objects.requireNonNull(getActivity())), 0, 0);
        clMineFragmentActionbar.setLayoutParams(layoutParams);
    }

    @Override
    public void refreshData(Bundle extras) {

    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    public void initData(Bundle extras) {

    }

    @Override
    public void onResume() {
        super.onResume();
        initPreview();
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @OnClick({R.id.iv_mine_fragment_right_setting, R.id.cl_mine_fragment_user, R.id.tv_mine_fragment_creation_center,
            R.id.tv_mine_fragment_browse_records, R.id.tv_mine_fragment_favorites, R.id.tv_mine_fragment_like_record, R.id.tv_mine_fragment_download_management,
            R.id.tv_mine_fragment_comment_management, R.id.tv_mine_fragment_feed_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_mine_fragment_right_setting:
                startSettingActivity();
                break;
            case R.id.cl_mine_fragment_user:
                if (UserManager.getInstance().isLogin()) {
                    startAccountInformationActivity();
                } else {
                    startLoginActivity();
                }
                break;
            case R.id.tv_mine_fragment_creation_center:
                if (UserManager.getInstance().isLogin()) {
                    startCreationCenterActivity();
                } else {
                    startLoginActivity();
                }
                break;
            case R.id.tv_mine_fragment_browse_records:
                if (UserManager.getInstance().isLogin()) {
                    startBrowseRecordsActivity();
                } else {
                    startLoginActivity();
                }
                break;
            case R.id.tv_mine_fragment_favorites:
                if (UserManager.getInstance().isLogin()) {
                    startFavoritesActivity();
                } else {
                    startLoginActivity();
                }
                break;
            case R.id.tv_mine_fragment_like_record:
                if (UserManager.getInstance().isLogin()) {
                    startLikeRecordActivity();
                } else {
                    startLoginActivity();
                }
                break;
            case R.id.tv_mine_fragment_download_management:
                if (UserManager.getInstance().isLogin()) {
                    startDownloadManagementActivity();
                } else {
                    startLoginActivity();
                }
                break;
            case R.id.tv_mine_fragment_comment_management:
                if (UserManager.getInstance().isLogin()) {
                    startCommentManagementActivity();
                } else {
                    startLoginActivity();
                }
                break;
            case R.id.tv_mine_fragment_feed_back:
                startFeedBackActivity();
                break;
            default:
                break;
        }
    }

    @Override
    public void startLoginActivity() {
        intent2Activity(LoginActivity.class);
    }

    @Override
    public void startAccountInformationActivity() {
        intent2Activity(AccountInformationActivity.class);
    }

    @Override
    public void startSettingActivity() {
        intent2Activity(SettingActivity.class);
    }

    @Override
    public void startCreationCenterActivity() {
        intent2Activity(CreationCenterActivity.class);
    }

    @Override
    public void startBrowseRecordsActivity() {
        intent2Activity(BrowseRecordsActivity.class);
    }

    @Override
    public void startFavoritesActivity() {
        intent2Activity(FavoritesActivity.class);
    }

    @Override
    public void startLikeRecordActivity() {
        intent2Activity(LikeRecordActivity.class);
    }

    @Override
    public void startDownloadManagementActivity() {
        intent2Activity(DownloadManagementActivity.class);
    }

    @Override
    public void startCommentManagementActivity() {
        intent2Activity(CommentManagementActivity.class);
    }

    @Override
    public void startFeedBackActivity() {
        intent2Activity(FeedBackActivity.class);
    }

    @Override
    public void initPreview() {
        boolean isLogin = UserManager.getInstance().isLogin();
        if (isLogin) {
            tvMineFragmentLoginRegister.setVisibility(View.GONE);
            llMineFragmentUserInfo.setVisibility(View.VISIBLE);
            getPresenter().getPersonInfo();
        } else {
            tvMineFragmentLoginRegister.setVisibility(View.VISIBLE);
            llMineFragmentUserInfo.setVisibility(View.GONE);
            Glide.with(this).load(R.drawable.bg_place_holder_nine_five)
                    .apply(new RequestOptions().circleCrop()).into(ivMineFragmentUserAvatar);
            showLayoutStatus(1);
        }
    }

    @Override
    public void initUserInfoData2View(UserInfoBean successOb) {
        if (successOb == null || successOb.user == null) {
            return;
        }
        UserInfoBean.UserBean userBean = successOb.user;
        /*
         * init avatar
         */

        Glide.with(this).load(ResourcesUtils.getInstance().getImagePath(userBean.picUrl)).apply(new RequestOptions().circleCrop()
                .placeholder(R.drawable.icon_default_avatar)
                .error(R.drawable.icon_default_avatar)
        ).into(ivMineFragmentUserAvatar);
        /*
         * init name
         */
        tvMineFragmentUserName.setText(StringUtils.getInstance().getNotEmptyMsg(userBean.username));
        /*
         * down count and like count
         */
        tvMineFragmentUserStarDownload.setText(String.format(StringUtils.getInstance().getString(R.string.tv_mine_fragment_user_star_download),
                userBean.praiseCount
                , userBean.downCount));
    }

}
